package gd.qaq.blue.locationleader;

import gd.qaq.blue.locationleader.cmd.LocationLeaderCommands;
import gd.qaq.blue.locationleader.listener.LocationLeaderListener;
import gd.qaq.blue.locationleader.util.JDBCUtils;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Map;

public final class LocationLeader extends JavaPlugin {
    public static FileConfiguration config;
    public static JDBCUtils jdbcUtils;
    public static LocationLeader plugin;

    @Override
    public void onEnable() {
        // Plugin startup logic
        plugin = this;
        loadDatas();
        LocationLeader.jdbcUtils = new JDBCUtils("plugins\\LocationLeader\\Locations.db");
        LocationLeader.jdbcUtils.isShow = true;
        Bukkit.getPluginCommand("locationLeader").setExecutor(new LocationLeaderCommands());
        Bukkit.getPluginManager().registerEvents(new LocationLeaderListener(), this);
        Map allCaches = jdbcUtils.getAllCaches();
        if (allCaches != null && allCaches.size() > 0) {
            LocationLeaderListener.playerUUIDAndTarget.putAll(allCaches);
//            plugin.getLogger().info("map:" + LocationLeaderListener.playerUUIDAndTarget);
        }
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }

    public static void sendSubtitleToPlayer(final Player p, final String subtitle) {
        if (!config.getBoolean("plugin.enableTitle")) {
            success(p, subtitle);
        } else {
            p.sendTitle(colorFormat("&b"), subtitle, 40, 60, 40);
        }
    }

    public static void sendSubtitleToPlayer(final Player p, final String subtitle, int stay) {
        if (!config.getBoolean("plugin.enableTitle")) {
            if (StringUtils.isNotBlank(subtitle)) {
                success(p, subtitle);
            }
        } else {
            p.sendTitle(colorFormat("&b"), subtitle, 40, stay, 40);
        }
    }

    private void loadDatas() {
        if (!this.getDataFolder().exists()) {
            this.getDataFolder().mkdirs();
            this.saveDefaultConfig();
        }
        config = this.getConfig();
    }

    public void reload(CommandSender p) {
        if (p.isOp()) {
            try {
                this.reloadConfig();
                config = this.getConfig();
                Map allCaches = jdbcUtils.getAllCaches();
                if (allCaches != null && allCaches.size() > 0) {
                    LocationLeaderListener.playerUUIDAndTarget.putAll(allCaches);
                    plugin.getLogger().info("map:" + LocationLeaderListener.playerUUIDAndTarget);
                }
                success(p, "插件重载成功");
            } catch (Exception e) {
                e.printStackTrace();
                error(p, "插件重载失败!");
            }
        }
    }

    public static String colorFormat(String str) {
        return str.replace("&", "§");
    }

    public static void success(CommandSender p, String str) {
        p.sendMessage(config.getString("plugin.prefix.success") + colorFormat(str));
    }

    public static void error(CommandSender p, String str) {
        p.sendMessage(config.getString("plugin.prefix.error") + colorFormat(str));
    }


}
